//
//  ZqlOriginalView.swift
//  ZqlSinaWeiBo
//
//  Created by 赵乾龙 on 16/2/21.
//  Copyright © 2016年 Mr.zhao. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class ZqlOriginalView: UIView {

    //添加约束对象属性
    var bottomConstraint:Constraint?
   
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        self.backgroundColor = UIColor.whiteColor()
        
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加模型属性
    var model:ZqlStatusModel?{
        didSet{
            iconImage.sd_setImageWithURL(model?.user?.profile_url)
            nameLable.text =  model?.user?.name
            mbrank_image.image = model?.user?.mbrank_image
            textLabel.text = model?.text
            //给配图数组赋值
            pictureView.imageUrls = model?.imageUrls
            
            //卸载老的约束对象
            self.bottomConstraint?.uninstall()
            //根据是否有配图重新设置约束
            if let imageUrls = model?.imageUrls where imageUrls.count != 0 {
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(cellMargin).constraint
                })

            }else{
                //原创微博底部相对于微博正文进行约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    // constraint 获取自动布局的约束对象
                    self.bottomConstraint = make.bottom.equalTo(textLabel.snp_bottom).offset(cellMargin).constraint
                })

            }
        }
    }
    
    //添加子视图
    private func setUpUI(){
        addSubview(iconImage)
        addSubview(nameLable)
        addSubview(timeLable)
        addSubview(soureLable)
        addSubview(mbrank_image)
        addSubview(verified_type_image)
        addSubview(textLabel)
        addSubview(pictureView)
        addSubview(sepView)
        
        sepView.backgroundColor = UIColor.yellowColor()
        
        //添加yueshu 
        iconImage.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(cellMargin)
            make.top.equalTo(sepView.snp_bottom).offset(cellMargin)
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        iconImage.layer.cornerRadius = 20
        iconImage.layer.masksToBounds = true
        
        nameLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImage.snp_right).offset(cellMargin)
            make.top.equalTo(iconImage.snp_top)
        }
        timeLable.snp_updateConstraints { (make) -> Void in
            make.left.equalTo(iconImage.snp_right).offset(cellMargin)
            make.bottom.equalTo(iconImage.snp_bottom)
        }
        soureLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLable.snp_right).offset(cellMargin)
            make.top.equalTo(timeLable.snp_top)
        }
        mbrank_image.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLable.snp_right).offset(cellMargin)
            make.top.equalTo(nameLable.snp_top)
        }
        verified_type_image.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(iconImage.snp_right)
            make.bottom.equalTo(iconImage.snp_bottom)
        }
        textLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(cellMargin)
            make.left.equalTo(iconImage.snp_left)
            make.right.equalTo(self.snp_right).offset(-cellMargin)

        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(textLabel.snp_bottom).offset(cellMargin)
            make.left.equalTo(textLabel.snp_left)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        
        
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.height.equalTo(10)
            
        }
        

        
        //添加底部约束
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(cellMargin).constraint
        }
    }
    
    //懒加载子视图
    
    //两个微博间的间距
    private lazy var sepView:UIView = UIView()
    
    //头像
    private lazy var iconImage:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    //用户名
    private lazy var nameLable:UILabel = UILabel(text: "哪有真爱", fontSize: 16, textColor: themeColor)
    
    //微博发布时间
    private lazy var timeLable:UILabel = UILabel(text: "21:15", fontSize: 14, textColor: UIColor.lightGrayColor())
    
    //微博来源
    private lazy var soureLable:UILabel = UILabel(text: "陈冠希", fontSize: 14, textColor: UIColor.lightGrayColor())
    
    //用户等级
    private lazy var mbrank_image:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    //用户认证类型
    private lazy var verified_type_image:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //微博正文
    private lazy var textLabel:UILabel = UILabel(text: "曾经沧海难为水,除却巫山不是云", fontSize: 16, textColor: UIColor.darkGrayColor(),alignment: NSTextAlignment.Left)
    
    //微博配图
    private lazy var pictureView:ZqlPictureView = ZqlPictureView()
    
}
